package handlers

import (
	"encoding/xml"
	"net/http"

	"homepage-backend/internal/config"
	"homepage-backend/internal/services"
)

// SitemapHandler 处理站点地图请求
func SitemapHandler(w http.ResponseWriter, r *http.Request) {
	sites, err := services.LoadSites(config.GlobalConfig.SitesFilePath)
	if err != nil {
		http.Error(w, "Failed to load sites", http.StatusInternalServerError)
		return
	}

	siteMap := services.GenerateSiteMap(sites)

	w.Header().Set("Content-Type", "application/xml")
	w.Write([]byte(xml.Header))
	xml.NewEncoder(w).Encode(siteMap)
}
